package com.cssy.xxkj.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.authority.dao.AppSqlConfig;
import com.cssy.authority.service.IAuthorityService;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.authority.service.impl.AuthorityServiceImpl;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicAllocationDO;
import com.cssy.xxkj.logic.entity.LogicAllocationItemDO;
import com.cssy.xxkj.logic.service.ILogicAllocationService;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.json.OutJsonGrid;

/**
 * 调拨业务逻辑
 * @author zeyong.gui
 *
 */
@Controller
@RequestMapping("/logic/allocation")
public class AllocationController extends BaseController<LogicAllocationDO> {

	private static final String MANAGER = "/xxkj/logic/allocat/AllocationManager";
	private static final String EIDT = "/xxkj/logic/allocat/AllocationOperation";
	
	@Resource(name="logicAllocationService")
	private ILogicAllocationService logicAllocationService;
	
	@Resource(name="basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		//入库仓
		model.addAttribute("inwhList",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		//出库仓
		List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
		model.addAttribute("ouwhList",list);
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs=this.oftenMothedUtil.getNoFinishConditions(scs, "zd002Status", BillStatusUtil.ACT_BILL_STATUS_ZTWO);
		Map<String, Object> whMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService, "zd004InWhCode");
		scs.addCondition(whMap.get("option")+"",(List<String>)whMap.get("roleWhList"));
		SearchResult result = logicAllocationService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if (OperationFlag.ADD.equals(flag)) {
			model.addAttribute("flag", "ADD");
		} else {
			LogicAllocationDO act = logicAllocationService.findListById(id);
			model.addAttribute("act", act);
			if(act!=null&&StringUtils.isNotBlank(act.getZd007Aegnt())){
				SqlMapClient sqlMapClient = AppSqlConfig.getSqlMapInstance();
				IAuthorityService ias = new AuthorityServiceImpl(sqlMapClient);
				com.cssy.authority.entity.User u = ias.getByUserName(act.getZd007Aegnt());
				model.addAttribute("userName", u.getRealname());
			}


			
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicAllocationDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String dataItem = request.getParameter("dataitem");
			if(StringUtils.isBlank(dataItem)){
				bodyMap.isFail();
				bodyMap.configWebInfor("产品列表不能为空！");
				return bodyMap.modelMap;
			}
			logicAllocationService.allocationSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(StringUtils.isBlank(field)){
					continue;
				}else{
					logicAllocationService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	
	/**
	 * 查询调拨发起单的行记录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String actId = request.getParameter("actId");
		if(StringUtils.isNotBlank(actId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("actId", actId);
			List<LogicAllocationItemDO> actmList = logicAllocationService.findItemListByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(actmList);
			out.printJSon(response);
		}
	}

}
